package com.xueyi.system.material.controller;

import com.xueyi.system.BaseDestroyRecords.domain.dto.BaseDestroyRecordsDto;
import com.xueyi.system.BaseDestroyRecords.domain.query.BaseDestroyRecordsQuery;
import com.xueyi.system.BaseDestroyRecords.service.IBaseDestroyRecordsService;
import com.xueyi.system.auxiliary.domain.dto.AuxiliaryDto;
import com.xueyi.system.auxiliary.domain.po.AuxiliaryPo;
import com.xueyi.system.auxiliary.domain.query.AuxiliaryQuery;
import com.xueyi.system.auxiliary.service.IAuxiliaryService;
import com.xueyi.system.auxiliaryReceive.domain.dto.AuxiliaryReceiveDto;
import com.xueyi.system.auxiliaryReceive.domain.query.AuxiliaryReceiveQuery;
import com.xueyi.system.auxiliaryReceive.service.IAuxiliaryReceiveService;
import com.xueyi.system.material.domain.query.MaterialQuery;
import com.xueyi.system.material.domain.dto.MaterialDto;
import com.xueyi.system.material.service.IMaterialService;
import com.xueyi.common.web.entity.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 投入品库管理 业务处理
 *
 * @author yanhe
 */
@RestController
@RequestMapping("/material")
public class MaterialController extends BaseController<MaterialQuery, MaterialDto, IMaterialService> {

    @Autowired
    private IMaterialService materialService;

    @Autowired
    private IBaseDestroyRecordsService baseDestroyRecordsService;

    @Autowired
    private IAuxiliaryService auxiliaryService;

    @Autowired
    private IAuxiliaryReceiveService auxiliaryReceiveService;



    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "投入品库" ;
    }

    /**
     * 查询投入品库列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.MATERIAL_LIST)
    public AjaxResult list(MaterialQuery material) {
        startPage();
        List<MaterialDto> materialDtos = materialService.selectList(material);
        for (MaterialDto materialDto : materialDtos) {
            BigDecimal entryQuantity = new BigDecimal("0");
            BigDecimal outQuantity = new BigDecimal("0");
            BigDecimal desQuantity = new BigDecimal("0");
            AuxiliaryQuery auxiliaryQuery = new AuxiliaryQuery();
            auxiliaryQuery.setMaterialId(materialDto.getId());
            auxiliaryQuery.setDelFlag(0L);
            List<AuxiliaryDto> auxiliaryDtos = auxiliaryService.selectList(auxiliaryQuery);
            for (AuxiliaryDto auxiliaryDto : auxiliaryDtos) {
                entryQuantity = entryQuantity.add(auxiliaryDto.getAuxiliaryNum());
            }
            AuxiliaryReceiveQuery auxiliaryReceiveQuery = new AuxiliaryReceiveQuery();
            auxiliaryReceiveQuery.setMaterialId(materialDto.getId().toString());
            auxiliaryReceiveQuery.setDelFlag(0L);
            List<AuxiliaryReceiveDto> auxiliaryReceiveDtos = auxiliaryReceiveService.selectList(auxiliaryReceiveQuery);
            for (AuxiliaryReceiveDto auxiliaryReceiveDto : auxiliaryReceiveDtos) {
                outQuantity = outQuantity.add(auxiliaryReceiveDto.getAuxiliaryNum());
            }

            BaseDestroyRecordsQuery destroyRecordsQuery = new BaseDestroyRecordsQuery();
            destroyRecordsQuery.setRuinId(materialDto.getId().toString());
            destroyRecordsQuery.setDelFlag(0L);
            List<BaseDestroyRecordsDto> baseDestroyRecordsDtos = baseDestroyRecordsService.selectList(destroyRecordsQuery);
            for (BaseDestroyRecordsDto recordsDto : baseDestroyRecordsDtos) {
                desQuantity= desQuantity.add(recordsDto.getRuinNum());
            }
            materialDto.setEntryQuantity(entryQuantity);
            materialDto.setOutQuantity(outQuantity);
            materialDto.setDesQuantity(desQuantity);
            BigDecimal subtract = entryQuantity.subtract(outQuantity);
            BigDecimal subtract1 = subtract.subtract(desQuantity);
            materialDto.setQuantity(subtract1);
            materialService.update(materialDto);
        }
        return getDataTable(materialDtos);
    }

    /**
     * 查询投入品库详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.MATERIAL_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 投入品库导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.MATERIAL_EXPORT)
    @Log(title = "投入品库管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, MaterialQuery material) {
        super.export(response, material);
    }

    /**
     * 投入品库新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.MATERIAL_ADD)
    @Log(title = "投入品库管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody MaterialDto material) {
        return super.add(material);
    }

    /**
     * 投入品库修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.MATERIAL_EDIT)
    @Log(title = "投入品库管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody MaterialDto material) {
        return super.edit(material);
    }

    /**
     * 投入品库修改状态
     */
    @Override
    @PutMapping("/status")
    @RequiresPermissions(value = {Auth.MATERIAL_EDIT, Auth.MATERIAL_ES}, logical = Logical.OR)
    @Log(title = "投入品库管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody MaterialDto material) {
        return super.editStatus(material);
    }

    /**
     * 投入品库批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.MATERIAL_DEL)
    @Log(title = "投入品库管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取投入品库选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 投入品库管理 - 列表 */
        String MATERIAL_LIST = "system:material:list" ;
        /** 系统 - 投入品库管理 - 详情 */
        String MATERIAL_SINGLE = "system:material:single" ;
        /** 系统 - 投入品库管理 - 新增 */
        String MATERIAL_ADD = "system:material:add" ;
        /** 系统 - 投入品库管理 - 修改 */
        String MATERIAL_EDIT = "system:material:edit" ;
        /** 系统 - 投入品库管理 - 修改状态 */
        String MATERIAL_ES = "system:material:es" ;
        /** 系统 - 投入品库管理 - 删除 */
        String MATERIAL_DEL = "system:material:delete" ;
        /** 系统 - 投入品库管理 - 导出 */
        String MATERIAL_EXPORT = "system:material:export" ;
    }
}
